<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfPropelParanoidBehavior.class.php 15813 2009-02-26 14:13:28Z fabien $
 */
class sfPropelBranchBehavior
{


    public function preSave($object, $con = null)
    {
    	if(sfContext::getInstance()->getUser()->hasCredential('super_admin')){
    		return true;
    	}
    	
        if($object instanceof sfGuardUser){
            $object = $object->getProfile();
        }

        $object->setBranchId( sfContext::getInstance()->getUser()->getBranchId() );

        //print_R($object);exit;
        //$object->save();

        return true;
    }

    public function doSelectStmt($class,Criteria $criteria, $con = null){
        if(!sfContext::getInstance()->getUser()->isAuthenticated()){
            return false;
        }
        if(sfConfig::get('sfPropelBranchBehavior_stop',false)){
            sfConfig::set('sfPropelBranchBehavior_stop',false);
            return false;
        }
        if($class=='BasesfGuardUserPeer'){
            $class = 'BaseUserProfilePeer';
            $joins = $criteria->getJoins();
            if(count($joins) == 0 ){
                $criteria->addJoin(sfGuardUserPeer::ID , UserProfilePeer::USER_ID );
            }
        }

        //var_dump(constant($class.'::BRANCH_ID'));exit;
        $criteria->add( constant($class.'::BRANCH_ID'),  sfContext::getInstance()->getUser()->getBranchId() );

        return false;
    }

}
